package com.baselibrary.utils

import android.app.Activity
import android.text.TextUtils
import android.view.Gravity
import com.baselibrary.bean.ShareItemBean
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.view.share.SharePopupWindow
import com.umeng.socialize.bean.SHARE_MEDIA

/**
 *@author:hanke
 *2022/8/5
 *desc:
 */
object ShareManager {
    fun showSharePopWindow(activity: Activity, url: String, cover: String, title: String, des: String, onClickListener: SharePopupWindow.OnShareClickListener): SharePopupWindow? {
        //如果文案过长，会导致分享不出去，这里做个截取
        val des = if (!TextUtils.isEmpty(des)) {
            ""
        } else {
            ""
        }
        //如果文案过长，会导致分享不出去，这里做个截取
        val title = if (!TextUtils.isEmpty(title)) {
           ""
        } else {
            ""
        }
        val shareWindow = SharePopupWindow(activity)
        shareWindow.setListener(object : SharePopupWindow.OnShareClickListener {
            override fun onShareClick(type: Int) {
                when (type) {
                    ShareItemBean.WX -> {
                        SensorsDataManager.instance.setShareWay("微信好友")
                        UMShareUtils.shareWeb(activity, SHARE_MEDIA.WEIXIN, url, cover, title, des)
                        onClickListener.onShareClick(type)
                    }
                    ShareItemBean.WXZone -> {
                        SensorsDataManager.instance.setShareWay("朋友圈")
                        UMShareUtils.shareWeb(activity, SHARE_MEDIA.WEIXIN_CIRCLE, url, cover, title, des)
                        onClickListener.onShareClick(type)
                    }
                    ShareItemBean.POSTER -> {
                        shareWindow.dismiss()
                        SensorsDataManager.instance.setShareWay("分享海报")
                        onClickListener.onShareClick(type)
                    }
                    ShareItemBean.DOWN -> {
                        onClickListener.onShareClick(type)
                    }
                    ShareItemBean.REPORT -> {
                        onClickListener.onShareClick(type)
                    }
                }
                SensorsDataManager.instance.forwarding(title)
            }

            override fun onShareFriendClick(bean: ShareItemBean?) {
                onClickListener.onShareFriendClick(bean)
            }

        })
        shareWindow.showAtLocation(activity.window.decorView, Gravity.BOTTOM, 0, 0)
        return shareWindow
    }

}